<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="confirm"
    @cancel="cancel"
  />
</template>

<script>
import { editUserProfileAPI } from '@/api'
import dayjs from '@/utils/dayjs'
export default {
  name: '',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    cancel() {
      this.$emit('close')
    },
    async confirm(value) {
      try {
        this.$toast.loading({
          message: '修改生日中...',
          forbidClick: true
        })
        // 1. 发请求
        await editUserProfileAPI({
          birthday: dayjs(value).format('YYYY-MM-DD')
        })
        // 2. 更新视图
        this.$emit('input', dayjs(value).format('YYYY-MM-DD'))
        // 3. 关闭弹窗
        this.$emit('close')
        // 4. 提示
        this.$toast.success('修改生日成功')
      } catch (error) {
        this.$toast.fail('修改生日失败')
      }
    }
  }
}
</script>

<style scoped></style>
